import { login, getInfo } from '@/api/user'    //logout
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLEYPE: (state, roleype) => {
    console.log(roleype)
    state.roleype = roleype
  }
}

const actions = {
  // // user login
  // login({ commit }, userInfo) {
  //   const { username, password, verifycode } = userInfo
  //   return new Promise((resolve, reject) => {
  //     login({ username: username.trim(), password: password, verifycode: verifycode }).then(response => {
  //       const { data } = response
  //       commit('SET_TOKEN', data)
  //       setToken(data)
  //       resolve()
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },
  login({ commit }, userInfo) {
    const { userName ,password } = userInfo
    return new Promise((resolve, reject) => {
      login({ userName: userName.trim(), password: password }).then(response => {
        if(response.code===1){
        console.log('response: ', response);
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
        }else{
           console.log('登录未成功')
        }
        // console.log('login__response: ', response);
        // const { data } = response
        // commit('SET_TOKEN', data)
        // setToken(data)
        // resolve()
      }).catch(error => {
        // console.log('登录错误')
        reject(error)
      })
    })
  },

  // get user info 登录验证
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          reject('登录验证失败，请重新登录')
        } 
        const { userName, headIcon ,roleype} = data
        // commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        commit('SET_NAME', userName)
        commit('SET_ROLEYPE', roleype)
        commit('SET_AVATAR', headIcon)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout 退出登录
  // logout({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     logout(state.token).then(() => {
  //       removeToken() // must remove  token  first
  //       resetRouter()
  //       commit('RESET_STATE')
  //       resolve()
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // remove token 删除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

